import services from '@/services/writeOff'; // 假设新的服务接口已经定义好
import {
  ActionType,
  PageContainer,
  ProDescriptionsItemProps,
  ProTable,
} from '@ant-design/pro-components';
import { Button, message } from 'antd';
import React, { useRef, useState } from 'react';
import CreateForm, { CreateFormValueType } from './components/CreateForm';
import UpdateForm, { UpdateFormValueType } from './components/UpdateForm';
import dayjs from 'dayjs';

const { createCourseUsed, updateCourseUsed, queryCourseUsed } =
  services.WriteOffController;


/**
 * 新增教师
 * @param fields
 */
const handleCreateStudent = async (fields: CreateFormValueType) => {
  console.log(fields);
  try {
    await createCourseUsed(fields);
    message.success('学生新增成功');
    return true;
  } catch (error) {
    message.error('学生新增失败');
    return false;
  }
};


const StudentList: React.FC<unknown> = () => {
  const [createModalVisible, handleCreateModalVisible] = useState<boolean>(false);
  const [updateModalVisible, handleUpdateModalVisible] = useState<boolean>(false);
  const actionRef = useRef<ActionType>();
  const [stepFormValues, setStepFormValues] = useState({});

  const columns: ProDescriptionsItemProps<CourseUsedAPI.CourseUsed>[] = [
    {
      title: '划课编号',
      dataIndex: 'id',
    }, {
      title: '教师',
      dataIndex: 'teacherName',
    },{
      title: '学生',
      dataIndex: 'studentName',
    }, {
      title: '教室',
      dataIndex: 'classroomName',
    }, {
      title: '课程类型',
      dataIndex: 'courseTypeName',
    },  {
      title: '科目',
      dataIndex: 'majorName',
    }, {
      title: '上课时间',
      dataIndex: 'startTime',
      valueType: 'dateTime',
    }, {
      title: '下课时间',
      dataIndex: 'endTime',
      valueType: 'dateTime',
    }, {
      title: '划课数',
      dataIndex: 'courseUsedCount',
    }, {
      title: '备注',
      dataIndex: 'comment',
    }, {
      title: '创建时间',
      dataIndex: 'createTime',
      valueType: 'dateTime',
    }, {
      title: '登记人',
      dataIndex: 'createBy',
    }, {
      title: '更新时间',
      dataIndex: 'updateTime',
      valueType: 'dateTime',
    }, {
      title: '变更人',
      dataIndex: 'updatedBy',
    }, {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => (
        <>
          <a
            onClick={() => {
              setStepFormValues({...record, birthday: dayjs(record.birthday)});
              handleUpdateModalVisible(true);
            }}
          >
            编辑
          </a>
        </>
      ),
    },
  ];

  return (
    <PageContainer
      header={{
        title: '划课管理',
      }}
    >
      <ProTable<StudentAPI.Student>
        headerTitle="查询表格"
        actionRef={actionRef}
        rowKey="studentId"
        search={{
          labelWidth: 120,
        }}
        toolBarRender={() => [
          <Button
            key="1"
            type="primary"
            onClick={() => handleCreateModalVisible(true)}
          >
            新建
          </Button>,
        ]}
        request={async (params, sorter, filter) => {
          const { data, success } = await queryCourseUsed(
            {
              'pager': {
              pageNum: params.current,
              pageSize: params.pageSize,
            }}
          );
          return {
            data: data?.result || [],
            success,
          };
        }}
        columns={columns}
      />

      <CreateForm
        onSubmit={async (value) => {
          console.log(value);
          const success = await handleCreateStudent(value);
          if (success) {
            handleCreateModalVisible(false);
            if (actionRef.current) {
              actionRef.current.reload();
            }
          }
        }}
        onCancel={() => {
          handleCreateModalVisible(false);
        }}
        visible={createModalVisible}
      />

      <UpdateForm
        onSubmit={async (value) => {
          const success = await updateStudent(value);
          if (success) {
            handleUpdateModalVisible(false);
            setStepFormValues({});
            if (actionRef.current) {
              actionRef.current.reload();
            }
          }
        }}
        onCancel={() => {
          handleUpdateModalVisible(false);
          setStepFormValues({});
        }}
        visible={updateModalVisible}
        values={stepFormValues}
      />


    </PageContainer>
  );
};

export default StudentList;
